﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using MC.RideEvent.Mvc.Models.Entity;
using MC.RideEvent.Mvc.Models.ViewModel;
using AutoMapper;

namespace MC.RideEvent.Mvc.Models.Repository
{
    public class VehiculeRepository : IVehiculeRepository
    {
        private RideEventDBEntities _context;

        public VehiculeRepository()
        {
            _context = new RideEventDBEntities();
        }

        /// <summary>
        /// This method return the vehicule of user
        /// whose userId parameter is matched.
        /// </summary>
        /// <param name="memberId"></param>
        /// <returns></returns>
        public VehiculeViewModel GetVehiculeById(int vehiculeId)
        {
            var vehicule = (from v in _context.Vehicules
                            where v.Id == vehiculeId
                            select v).FirstOrDefault();

            var data = Mapper.Map<Vehicule, VehiculeViewModel>(vehicule);
            return data;
        }

        public List<VehiculeViewModel> GetVehiculesByMemberId(int memberId)
        {
            var list = (from v in _context.Vehicules
                        where v.MemberId == memberId
                        select v).ToList();

            var datas = AutoMapper.Mapper.Map<List<Vehicule>, List<VehiculeViewModel>>(list);
            return datas;
        }

        public bool SaveVehicule(VehiculeViewModel vehicule, bool isUpdate)
        {
            Vehicule v = null;

            if(isUpdate)
            {
                v = UpdateVehicule(vehicule);
                _context.Entry(v);
                _context.Entry(v).State = EntityState.Modified;
            }
            else
            {
                v = CreateVehicule(vehicule);
                _context.Vehicules.Add(v);
            }

            var result = Convert.ToBoolean(_context.SaveChanges());

            return result;
        }

        private Vehicule CreateVehicule(VehiculeViewModel vehicule)
        {
            var dateNow = DateTime.Now;

            var car = new Vehicule();
            car.Matricule = vehicule.Matricule;
            car.NumberPlaces = vehicule.NumberPlaces;
            car.MemberId = vehicule.MemberId;
            car.VehiculeModelId = vehicule.VehiculeModelId;
            car.DateCreation = dateNow;
            car.DateUpdate = dateNow;

            return car;
        }

        private Vehicule VehiculeById(int vehiculeId)
        {
            var v = (from ve in _context.Vehicules
                     where ve.Id == vehiculeId
                     select ve).FirstOrDefault();

            return v;
        }

        private Vehicule UpdateVehicule(VehiculeViewModel vehicule)
        {
            var v = VehiculeById(vehicule.Id);

            if(v != null)
            {
                v.Matricule = vehicule.Matricule;
                v.NumberPlaces = vehicule.NumberPlaces;
                v.VehiculeModelId = vehicule.VehiculeModelId;
                v.DateUpdate = DateTime.Now;
            }

            return v;
        }

        public List<CarModel> GetVehiculesMember(int memberId)
        {
            var datas = (from v in _context.Vehicules
                        where v.MemberId == memberId
                        select new CarModel
                                   {
                                       Id = v.Id,
                                       Matricule = v.Matricule,
                                       NumberPlacesMax = v.NumberPlaces,
                                       TitleBrand = v.VehiculeModel.VehiculeBrand.Title,
                                       TitleModel = v.VehiculeModel.Title
                                   }).ToList();
            return datas;
        }
    }
}
